import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import vueDevtools from 'vite-plugin-vue-devtools'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue({
      script: {
        defineModel: true,
      },
    }),
    vueDevtools(),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
    },
  },
  server: {
    proxy: {
      '/api': {
        target: 'http://localhost:8080',
        changeOrigin: true,
      },
    },
  },
  build: {
    // 1. 提高阈值（可选）
    chunkSizeWarningLimit: 1000, // kB

    // 2. 关键：手动分包
    rollupOptions: {
      output: {
        manualChunks(id) {
          // node_modules 统一拆
          if (id.includes('node_modules')) {
            if (id.includes('element-plus')) return 'element-plus'
            if (id.includes('@lottiefiles')) return 'lottie'
            if (id.includes('vue')) return 'vue'
            return 'vendor' // 其他 npm 包
          }
        },
      },
    },
  },
})
